package com.example.dailyrecord.base.service.impl;

import com.example.dailyrecord.base.dao.BaseDao;
import com.example.dailyrecord.base.entity.Page;
import com.example.dailyrecord.base.entity.SQLBuilder;
import com.example.dailyrecord.base.service.BaseService;
import com.example.dailyrecord.entity.StatusCode;
import com.example.dailyrecord.entity.User;
import com.example.dailyrecord.util.FunctionUtils;
import com.example.dailyrecord.util.PagingList;
import com.example.dailyrecord.util.RequestUtils;
import com.example.dailyrecord.util.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class BaseServiceImpl<T> implements BaseService<T> {
    @Autowired
    protected BaseDao<T> baseDao;
    @Autowired
    protected TokenUtils tokenUtils;

    public String getEmail(HttpServletRequest request) {
        String email = request.getParameter(User.EMAIL_NAME);
        if (StringUtils.isEmpty(email)) {
            String token = RequestUtils.getToken(request);
            return tokenUtils.parse(token).getData();
        }
        return email;
    }

    protected void startPage(HttpServletRequest request) {
        PageHelper.startPage(Integer.parseInt(request.getParameter("pageNum")),
                Integer.parseInt(request.getParameter("offset")));
    }

    protected void startPage(Page page) {
        PageHelper.startPage(page.getPageNum(), page.getOffset());
    }

    @Override
    public StatusCode insertRequest(HttpServletRequest request, HttpServletResponse response) {
        return null;
    }

    @Override
    public void deleteRequest(HttpServletRequest request, HttpServletResponse response) {
        String key = request.getParameter("id");
        delete(key);
    }

    @Override
    public T updateRequest(HttpServletRequest request, HttpServletResponse response) {
        return null;
    }

    @Override
    public T selectRequest(HttpServletRequest request, HttpServletResponse response) {
        String key = request.getParameter("id");
        return select(key);
    }


    @Override
    public PageInfo<T> getListRequest(HttpServletRequest request, HttpServletResponse response) {
        return null;
    }


    @Override
    public long countRequest(HttpServletRequest request, HttpServletResponse response) {
        return 0;
    }

    @Override
    public void insert(T t) {
        if (t == null) {
            return;
        }
        baseDao.insert(t);
    }

    @Override
    public void delete(String key) {
        if (key.contains(",")) {
            deleteFree(SQLBuilder.getInstance().in("id", key.split(",")));
        } else {
            baseDao.delete(key);
        }
    }

    @Override
    public void update(T key) {
        baseDao.update(key);
    }

    @Override
    public T select(String key) {
        return baseDao.select(key);
    }

    @Override
    public PageInfo<T> getList(T t, Page page) {
        PageHelper.startPage(page.getPageNum(), page.getLimit());
        return PageInfo.of(baseDao.getList(t));
    }

    @Override
    public void insertList(List<T> list) {
        if (FunctionUtils.isEmpty(list)) {
            return;
        }
        PagingList<T> pagingList = new PagingList<>(list, 300);
        while (pagingList.hasNext()) {
            baseDao.insertList(pagingList.next());
        }
    }

    @Override
    public void deleteFree(SQLBuilder builder) {
        baseDao.deleteFree(builder);
    }

    @Override
    public void updateFree(SQLBuilder builder) {
        baseDao.updateFree(builder);
    }

    @Override
    public void updateList(List<T> list) {
        baseDao.updateList(list);
    }

    @Override
    public void updateByBuilder(T key, SQLBuilder builder) {
        baseDao.updateByBuilder(key, builder);
    }

    @Override
    public T selectFree(SQLBuilder builder) {
        return baseDao.selectFree(builder);
    }

    @Override
    public List<T> selectList(SQLBuilder builder) {
        return baseDao.selectList(builder);
    }

    @Override
    public String selectStringFree(SQLBuilder builder) {
        return baseDao.selectStringFree(builder);
    }

    @Override
    public List<String> selectStringListFree(SQLBuilder builder) {
        return baseDao.selectStringListFree(builder);
    }

    @Override
    public List<T> selectListFree(SQLBuilder builder) {
        return baseDao.selectListFree(builder);
    }

    @Override
    public long countByBuilder(SQLBuilder builder) {
        return baseDao.countByBuilder(builder);
    }

    @Override
    public void truncate() {
        baseDao.truncate();
    }
}
